//
//  MJPhotoToolbar.m
//  FingerNews
//
//  Created by mj on 13-9-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MLPhotoToolbar.h"
#import "MLPhotoInfo.h"
#import "MBProgressHUD+Add.h"

#define BUTTON_WIDTH 20
#define BUTTON_HEIGTH 20
#define MARGIN 10
@interface MLPhotoToolbar()
{
    // 显示页码
    UILabel *_indexLabel;
    UIButton *_saveImageBtn;
}
@end

@implementation MLPhotoToolbar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)setPhotos:(NSArray *)photos
{
    _photos = photos;
    
    if (_photos.count > 1) {
        _indexLabel = [[UILabel alloc] init];
        _indexLabel.font = [UIFont boldSystemFontOfSize:20];
        _indexLabel.frame = self.bounds;
        _indexLabel.backgroundColor = [UIColor clearColor];
        _indexLabel.textColor = [UIColor whiteColor];
        _indexLabel.textAlignment = NSTextAlignmentCenter;
        _indexLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        [self addSubview:_indexLabel];
    }
    
    // 保存图片按钮
    
    _saveImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _saveImageBtn.frame = CGRectMake(CGRectGetWidth(self.bounds) - MARGIN - BUTTON_WIDTH, (CGRectGetHeight(self.bounds) - BUTTON_HEIGTH) / 2, BUTTON_WIDTH, BUTTON_HEIGTH);
    _saveImageBtn.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [_saveImageBtn setImage:[UIImage imageNamed:@"list_button.png"] forState:UIControlStateNormal];
   
    [_saveImageBtn addTarget:self action:@selector(saveImage) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_saveImageBtn];
    [self bringSubviewToFront:_saveImageBtn];
}

- (void)saveImage
{
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"保存到相册" otherButtonTitles:nil, nil];
    [sheet showInView:self];
}



#pragma mark actionSheet代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) { // 保存到手机
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            MLPhotoInfo *photo = _photos[_currentPhotoIndex];
            UIImageWriteToSavedPhotosAlbum(photo.photo.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        });

    }
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        [MBProgressHUD showSuccess:@"保存失败" toView:nil];
    } else {
        [MBProgressHUD showSuccess:@"成功保存到相册" toView:nil];
    }
}

- (void)setCurrentPhotoIndex:(NSUInteger)currentPhotoIndex
{
    _currentPhotoIndex = currentPhotoIndex;
    // 更新页码
    _indexLabel.text = [NSString stringWithFormat:@"%ld / %ld", _currentPhotoIndex + 1, _photos.count];
}

#pragma mark 动画显示隐藏
- (void)hideToolbar
{
    if (self == NO)
    {
        [UIView animateWithDuration:0.45 delay:0.0
                            options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                         animations:^(void)
         {
             self.alpha = 0.0f;
         }
                         completion:^(BOOL finished)
         {
             self.hidden = YES;
         }
         ];
    }
}

- (void)showToolbar
{
    if (self.hidden == YES)
    {
        [UIView animateWithDuration:0.45 delay:0.0
                            options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                         animations:^(void)
         {
             self.hidden = NO;
             self.alpha = 1.0f;
         }
                         completion:NULL
         ];
    }
}


- (void)hideIndexLabel
{
    if (_indexLabel.hidden == NO)
    {
        [UIView animateWithDuration:0 delay:0.0
                            options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                         animations:^(void)
         {
             _indexLabel.alpha = 0.0f;
         }
                         completion:^(BOOL finished)
         {
             _indexLabel.hidden = YES;
         }
         ];
    }
}
- (void)hideSaveButton
{
    if (_saveImageBtn.hidden == NO)
    {
        [UIView animateWithDuration:0.0 delay:0.0
                            options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                         animations:^(void)
         {
             _saveImageBtn.alpha = 0.0f;
         }
                         completion:^(BOOL finished)
         {
             _saveImageBtn.hidden = YES;
         }
         ];
    }
}

@end
